import Vue from "vue";
import VueRouter from "vue-router";
import Login from "@/components/Login.vue";
import Home from "@/components/Home.vue";

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(VueRouter);

const routes = [
  { path: "/admin-login", component: Login },
  {
    path: "/admin",
    component: Home,
    redirect: "/admin/welcome",
    children: [
      { path: "welcome", components: { main: ()=>import('@/components/Welcome.vue') } },
      { path: "users", components: { main: ()=>import('@/components/user/Users.vue') } },
      { path: "all-articles", components: { main: ()=>import('@/components/ArticleManagement/AllAarticles.vue') } },
      { path: "add-articles", components: { main: ()=>import('@/components/ArticleManagement/AddArticles.vue') } },
      { path: "revise-articles/:id", components: { main: ()=>import('@/components/ArticleManagement/EditArticles.vue') } },
      { path: "classified-articles", components: { main: ()=>import('@/components/ArticleManagement/ClassifiedArticles.vue') } },
    ],
  },
  {
    path: "/",
    redirect: "/index",
    component: () => import("@/views/Index.vue"),
    children: [
      {
        path: "index",
        redirect: '/index/cate/all',
        component: () => import("@/components/Blog/BlogMain.vue"),
        children: [
          {
            path: "cate/:id",
            component: () => import("@/components/Blog/BlogArticle.vue"),
          },
        ],
      },
      {
        path:'article/:id',
        component:()=>import('@/components/Blog/BlogArticleDeatail.vue'),
      }
    ],
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  NProgress.start()
  next()
})

router.afterEach(() => {
  NProgress.done()
})
// router.beforeEach((to,from,next)=>{
//   //to将要访问的路径
//   // from代表从哪一个路径跳转而来
//   // next是一个函数，表示放行
//   // next()  next('/login')
//   if(to.path === '/admin-login') return next()
//   //获取登录信息
//   const tokenSkr = window.sessionStorage.getItem('token')
//   if(!tokenSkr) return next('/admin-login')
//   next()
// })
export default router;
